package com.kx.yooli.kx.wechat.utils.uuid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author john_sqwen-childe.wen
 * date 2024/4/8
 * PROJECT_NAME wechat
 * description create RandomUtil by john_sqwen
 * and  value is
 */
public class RandomUtil {
    private final Logger log = LoggerFactory.getLogger(RandomUtil.class);
    private static final String CHAR_LIST = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    private static final int CHAR_LEN = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".length();
    private static final String NUMBER_CHAR_LIST = "0123456789";
    private static final int NUMBER_CHAR_LEN = "0123456789".length();
    private static final int DEFUALT_TIME_RANDOM = 10000000;

    public RandomUtil() {
    }

    public static String getRandomString(int length) {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < length; ++i) {
            sb.append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".charAt(random.nextInt(CHAR_LEN)));
        }

        return sb.toString();
    }

    public static String getNumberRandom(int length) {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < length; ++i) {
            sb.append("0123456789".charAt(random.nextInt(NUMBER_CHAR_LEN)));
        }

        return sb.toString();
    }

    public static String getUUIDRandom(boolean isCrossed) {
        String uuid = UUID.randomUUID().toString();
        return isCrossed ? uuid : uuid.replaceAll("-", "");
    }

    public static String generateTraceId() {
        int random = ThreadLocalRandom.current().nextInt();
        long epochSeconds = System.currentTimeMillis() / 1000L;
        return Long.toHexString((epochSeconds & 4294967295L) << 32 | (long)random & 4294967295L);
    }
}
